﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Partial Public Class SiteDepartment

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property

    Private _departmentList As New List(Of SiteDepartment)
    Public ReadOnly Property Childs() As List(Of SiteDepartment)
        Get
            If _departmentList Is Nothing Then
                _departmentList = New List(Of SiteDepartment)
            End If
            Return _departmentList
        End Get
    End Property

    Public ReadOnly Property Parent() As SiteDepartment
        Get
            If Me.ParentID > 0 Then
                Return GetByPrimaryKey(Me.ParentID)
            End If
            Return Nothing
        End Get
    End Property
#End Region

#Region "Constants"
    Private Const UNKNOWN_SITE As String = "Unknown Site"
    Private Const UNKNOWN_LOCATION As String = "Unknown Department"
    Private Const ACTIVE_STATUS As String = "Active"
    Private Const ALL_STATUS As String = "All"
    Private Const INACTIVE_STATUS As String = "Inactive"
#End Region

#Region "Public Section"

    Public Shared Function RetrieveAll() As IEnumerable(Of SiteDepartment)
        Return Database.Instance.SiteDepartments.OrderBy(Function(l) l.ID)
    End Function

    Public Shared Function GetTreeForSite(ByVal siteID As Integer) As IEnumerable(Of SiteDepartment)
        Dim result As New List(Of SiteDepartment)
        Dim site As SiteDepartment = SiteDepartment.GetByPrimaryKey(siteID)

        result.Add(site)

        Dim childs = RetrieveAllDepartmentsBySite(siteID)
        For Each item As SiteDepartment In childs
            site.Childs.Add(item)
        Next

        Return result
    End Function

    Public Shared Function GetTreeForSite(ByVal siteID As Integer, ByVal filter As String) As IEnumerable(Of SiteDepartment)
        Dim result As New List(Of SiteDepartment)
        Dim site As SiteDepartment = SiteDepartment.GetByPrimaryKey(siteID)
        result.Add(site)

        If IsNumeric(filter) Then
            Dim departmentID As Integer = Integer.Parse(filter)
            Dim department As SiteDepartment = SiteDepartment.GetByPrimaryKey(departmentID)
            site.Childs.Add(department)
        Else
            Dim filterStatus As Nullable(Of Boolean) = Nothing
            Dim childs = RetrieveAllDepartmentsBySite(siteID)

            If Not String.IsNullOrEmpty(filter) Then
                If filter = ACTIVE_STATUS Then
                    filterStatus = True
                ElseIf filter = INACTIVE_STATUS Then
                    filterStatus = False
                ElseIf filter = ALL_STATUS Then
                    filterStatus = Nothing
                End If
            End If

            For Each item As SiteDepartment In childs
                If filterStatus Is Nothing Or item.IsActive = filterStatus Then
                    site.Childs.Add(item)
                End If
            Next
        End If

        Return result
    End Function


    Public Shared Function GetTree() As IEnumerable(Of SiteDepartment)
        Return GetTree(String.Empty)
    End Function
    Public Shared Function GetTree(ByVal filter As String) As IEnumerable(Of SiteDepartment)
        Dim result As New List(Of SiteDepartment)

        If IsNumeric(filter) Then
            Dim departmentID As Integer = Integer.Parse(filter)
            Dim department As SiteDepartment = SiteDepartment.GetByPrimaryKey(departmentID)
            Dim site As SiteDepartment = SiteDepartment.GetByPrimaryKey(department.ParentID)
            site.Childs.Add(department)
            result.Add(site)
        Else
            Dim filterStatus As Nullable(Of Boolean) = Nothing
            Dim all = RetrieveAll()

            If Not String.IsNullOrEmpty(filter) Then
                If filter = ACTIVE_STATUS Then
                    filterStatus = True
                ElseIf filter = INACTIVE_STATUS Then
                    filterStatus = False
                ElseIf filter = ALL_STATUS Then
                    filterStatus = Nothing
                End If
            End If

            For Each item As SiteDepartment In all
                If item.Type = SiteDepartmentType.Site AndAlso Not result.Contains(item) Then
                    If filterStatus Is Nothing Or item.IsActive = filterStatus Then
                        result.Add(item)
                    End If
                Else
                    If item.Type = SiteDepartmentType.Department Then
                        Dim parent As SiteDepartment = all.SingleOrDefault(Function(sl) sl.ID = item.ParentID)
                        If parent IsNot Nothing Then
                            If filterStatus Is Nothing Or item.IsActive = filterStatus Then
                                parent.Childs.Add(item)
                            End If
                        End If
                    End If
                End If
            Next
        End If

        Return result
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As SiteDepartment
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From sl In db.SiteDepartments Where sl.ID = id
        Dim siteDepartmentEntity As SiteDepartment = result.FirstOrDefault()
        If siteDepartmentEntity IsNot Nothing Then
            siteDepartmentEntity.DBContext = db
        End If
        Return siteDepartmentEntity
    End Function

    Public Shared Function GetDepartmentByNameForSiteExcept(ByVal siteID As Integer, ByVal exceptDepartmentID As Integer, ByVal name As String) As SiteDepartment
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From sl In db.SiteDepartments Where sl.ParentID = siteID AndAlso sl.Description = name AndAlso sl.ID <> exceptDepartmentID
        Dim siteDepartmentEntity As SiteDepartment = result.FirstOrDefault()
        If siteDepartmentEntity IsNot Nothing Then
            siteDepartmentEntity.DBContext = db
        End If
        Return siteDepartmentEntity
    End Function


    Public Shared Function SearchDepartmentByNameForSite(ByVal siteID As Integer, ByVal prefixText As String) As IQueryable(Of SiteDepartment)
        Return From sl In Database.Instance.SiteDepartments Where sl.Type = SiteDepartmentType.Department AndAlso sl.Description Like String.Format("{0}*", prefixText) AndAlso sl.ParentID = siteID Order By sl.Description
    End Function


    Public Shared Function SearchDepartmentByName(ByVal prefixText As String) As IQueryable(Of SiteDepartment)
        Return From sl In Database.Instance.SiteDepartments Where sl.Type = SiteDepartmentType.Department AndAlso sl.Description Like String.Format("{0}*", prefixText) Order By sl.Description
    End Function

    Public Shared Function RetrieveAll(ByVal type As SiteDepartmentType) As IEnumerable(Of SiteDepartment)
        Return From sl In Database.Instance.SiteDepartments Where sl.Type = type Order By sl.Description
    End Function


    Public Shared Function RetrieveAllDepartmentsBySite(ByVal siteID As Integer) As IEnumerable(Of SiteDepartment)
        Return From sl In Database.Instance.SiteDepartments Where sl.Type = SiteDepartmentType.Department AndAlso sl.ParentID = siteID Order By sl.Description
    End Function

    Public Shared Function InsertSite(ByVal desc As String, ByVal addr As String, ByVal isActive As Boolean, ByVal timeZoneID As String) As SiteDepartment
        Dim db As EquipmentDataContext = Database.Instance
        Dim newSite As New SiteDepartment

        newSite.Description = desc
        newSite.Type = SiteDepartmentType.Site
        newSite.Address = addr
        newSite.IsActive = isActive
        newSite.TimeZoneID = timeZoneID

        db.SiteDepartments.InsertOnSubmit(newSite)
        Try
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
        Return newSite
    End Function

    Public Shared Function InsertDepartment(ByVal siteID As Integer, ByVal desc As String, ByVal addr As String, ByVal isActive As Boolean) As SiteDepartment
        Dim db As EquipmentDataContext = Database.Instance
        Dim newDepartment As New SiteDepartment

        newDepartment.ParentID = siteID
        newDepartment.Description = desc
        newDepartment.Type = SiteDepartmentType.Department
        newDepartment.Address = addr
        newDepartment.IsActive = isActive

        db.SiteDepartments.InsertOnSubmit(newDepartment)
        Try
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
        Return newDepartment
    End Function


    Public Shared Sub AddSite()
        Dim db As EquipmentDataContext = Database.Instance
        Dim newSite As New SiteDepartment

        newSite.Description = UNKNOWN_SITE
        newSite.Type = SiteDepartmentType.Site
        db.SiteDepartments.InsertOnSubmit(newSite)
        Try
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
    End Sub

    Public Shared Sub AddDepartment(ByVal siteID As Integer)
        Dim db As EquipmentDataContext = Database.Instance
        Dim newSite As New SiteDepartment

        newSite.Description = UNKNOWN_LOCATION
        newSite.Type = SiteDepartmentType.Department
        newSite.ParentID = siteID
        db.SiteDepartments.InsertOnSubmit(newSite)
        Try
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
    End Sub

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext

            Try
                _dataBase.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If

    End Sub

#End Region
End Class
